<?php
namespace Home\Controller;
use Think\Controller;

class AjaxItwController extends Controller{
	//Ajax入口
	function doAjax(){
		$funType = $_GET['funType'];
		$ajaxMsg = "Init message";
		
		switch($funType){
			case "waitAllItw": $thepage = $_GET['thepage']; $ajaxMsg = $this->waitItw($thepage); break;
			case "haveAllItw": $thepage = $_GET['thepage']; $ajaxMsg = $this->haveItw($thepage); break;
			case "sltItw":		$ajaxMsg = $this->sltItw($_GET['keyword']);	break;
			case "getOneItw": $ajaxMsg = $this->getOneItw($_GET['id']);	break;
			case "updateHaveItw":	$ajaxMsg=$this->updateHaveItw($_GET['data']); break;
		}
		
		$this->ajaxReturn($ajaxMsg);
	}
	
	function index(){
		header("Content-type:text/html; charset=utf-8");
		echo $this->getOneItw(303)."<br>";
		echo $this->getOneItw(295);
	}
	
	function updateHaveItw($data){
		$data = explode("~",$data);
		
		$data[5] = str_replace("T", " ", $data[5]);
		
		$mysql = new \Home\Common\MysqlOps();
		$set_=array(
				"name"	=>	"'$data[1]'",
				"sex"	=>	$data[2],
				"contact"	=>	"'$data[4]'",
				"job"	=>	"'$data[3]'",
				"itwer"	=>	"'$data[6]'",
				"thedate"	=>	"'$data[5]'",
				"result"	=>	"'$data[7]'",
				"other"	=>	"'$data[8]'"
		);
		$status = $mysql->updateItem("iry_interview", $set_, array("id"=>$data[0]));
		return $status?"success":"fail";
	}
	//获取特定条目信息
	function getOneItw($id){
		$itw = new \Home\Common\Interview();
		$info = $itw->getOneItw($id);
		$info['thedate'] = str_replace(" ", "T",$info['thedate']);
		$strInfo = "";
		foreach($info as $value){
			$strInfo .= $value."~";
		}
		$strInfo = substr($strInfo, 0,strlen($strInfo)-1);
		return $strInfo;
	}
	//待面试条目
	function waitItw($page){
		$itw = new \Home\Common\Interview();
		$arItw = $itw->getWait($page);

		//渲染获取结果
		$html = "<table cellpadding='0' cellspacing='0' id='example' class='info-table'>
				<tr><th width='40'>序号</th>
				<th width='50'>姓名</th>
				<th width='40'>性别</th>
				<th width='60'>联系方式</th>
				<th width='80'>职位</th>
				<th width='50'>面试官</th>
				<th width='80'>时间</th>
				<th width='40'>简历</th>
				<th width='60'>结果</th>
				<th width='40'> 处 理</th></tr>";
		foreach($arItw as $v){
			if($v[7] == "")	$download = "无";
			else $download = "<a href='../Index/downLoad?filename=$v[7]'>下载</a>";
			if($v[2]==1) $v[2] = "男"; else if($v[2]==2) $v[2] = "女"; else $v[2] = "0";

			$button = "<input type='button' value='处理' onclick=\"dealItw('show',$v[0],'$v[1]')\"  class=\"button-3\">";
			$html.="<tr><td>$v[0]</td><td>$v[1]</td><td>$v[2]</td><td>$v[3]</td><td>$v[4]</td>
			<td>$v[5]</td><td>$v[6]</td><td>$download</td><td>$v[8]</td><td>$button</td></tr>";
		}
		$html .= "</table>";

		return $html;
	}
	//已面试条目
	function haveItw($page){
		$itw = new \Home\Common\Interview();
		$arItw = $itw->getHave($page);

		//处理获取结果
		$html = "<table cellpadding='0' cellspacing='0' id='itw-table' class='info-table'>
				<tr><th width='40'>序号</th>
				<th width='50'>姓名</th>
				<th width='40'>性别</th>
				<th width='60'>联系方式</th>
				<th width='80'>职位</th>
				<th width='50'>面试官</th>
				<th width='80'>时间</th>
				<th width='40'>简历</th>
				<th width='60'>结果</th>
				<th width='40'>备注</th>
				<th width='40'>更改</th></tr>";
		foreach($arItw as $v){
			if($v[7] == "") $download = "丢失";
			else	$download = "<a href='../Index/downLoad?filename=$v[7]'>下载</a>";

			if($v[2]==1) $v[2] = "男"; else if($v[2]==2) $v[2] = "女"; else $v[2] = "缺失";

			$html.="<tr><td>$v[0]</td><td>$v[1]</td><td>$v[2]</td><td>$v[3]</td><td>$v[4]</td>
			<td>$v[5]</td><td>$v[6]</td><td>$download</td><td>$v[8]</td><td>$v[9]</td><td><button onClick='editHaveItw(\"run\",$v[0])'>更改</button></td></tr>";
		}
		$html .= "</table>";

		return $html;
	}
	//添加面试条目
	function addItw(){
		if(isset($_POST['itw-name'])){
			//获取前台信息
			$name		= strip_tags($_POST['itw-name']);
			$sex		= strip_tags($_POST['itw-sex']);
			$contact	= strip_tags($_POST['itw-contact']);
			$job		= strip_tags($_POST['itw-job']);
			$itwer		= strip_tags($_POST['itw-itwer']);
			$thedate	= strip_tags($_POST['itw-date']);
			$resume		= $_FILES['itw-resume'];

			$itwer = explode(",", "$itwer");
			$thedate = str_replace("T", " ", $thedate);
			//建立文件信息分析
			if($resume[tmp_name] != ""){
				$upload	=	new \Think\Upload();
				$upload->rootPath	=	'./Public/Resume/';
				$upload->savePath	=	'';
					
				$info	=	$upload->uploadOne($resume);
				if(!$info){
					$this->error($upload->getError());
				}else{
					$resume	=	"Public/Resume/".date("Y-m-d")."/".$info['savename'];
				}
			}else{
				$resume	=	"";
			}
				
			//写入数据库
			$itw = new \Home\Common\Interview();
			$result = $itw->addItem($name,$sex,$contact,$job,$itwer[0],$thedate,$resume);
				
			//分析操作结果
			if($result){
				$message = "<b>尊敬的$itwer[0],您有一份即将到来的面试</b><br>姓名:$name<br>时间:$thedate<br>职位:$job<br>联系方式:$contact<br>
				<a href='http://10.0.1.43/phpweb/iruiyou_web/index.php/home/index/ajaxindex.html'>点击查看详情</a>";
				SendMail($itwer[1], '睿游面试官提醒', $message);
				$this->redirect('/Home/Index/ajaxIndex');
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->error("请填写姓名");
		}
	}
	//处理面试条目
	function dealItw(){
		if(!isset($_POST['itw-id'])) $this->error('Not Found!');
		
		$id = strip_tags($_POST['itw-id']);
		$result = strip_tags($_POST['itw-result']);
		$other = strip_tags($_POST["itw-other"]);
		
		$itw = new \Home\Common\Interview();
		$result = $itw->dealItw($id, $result, $other);
		
		if($result) $this->redirect('/Home/Index/ajaxIndex');
		else $this->error('处理失败，请告知管理员!');
	}
	//搜索面试条目
	function sltItw($keyword){
		$itw = new \Home\Common\Interview();
		$arItw = $itw->selectItw($keyword);
		
		$html = "<table cellpadding='0' cellspacing='0' id='itw-table' class='info-table'>
				<tr><th width='40'>序号</th>
				<th width='50'>姓名</th>
				<th width='40'>性别</th>
				<th width='60'>联系方式</th>
				<th width='80'>职位</th>
				<th width='50'>面试官</th>
				<th width='80'>时间</th>
				<th width='40'>简历</th>
				<th width='60'>结果</th>
				<th width='40'>备注</th></tr>";
		
		foreach($arItw as $v){
			if($v[7] == "") $download = "丢失";
			else	$download = "<a href='../Index/downLoad?filename=$v[7]'>下载</a>";
				
			if($v[2]==1) $v[2] = "男"; else if($v[2]==2) $v[2] = "女"; else $v[2] = "缺失";
				
			$html.="<tr><td>$v[0]</td><td>$v[1]</td><td>$v[2]</td><td>$v[3]</td><td>$v[4]</td>
			<td>$v[5]</td><td>$v[6]</td><td>$download</td><td>$v[8]</td><td>$v[9]</td></tr>";
		}
		$html .= "</table>";
		
		return $html;
	}
}